<template>
	<view class="resourceList">
		<uni-row style="margin-top: 8rpx;margin-bottom: 20rpx;">
			<uni-col :span="2" style="padding-top: 7rpx;">
				<uni-icons type="list" size="32"></uni-icons>
			</uni-col>
			<uni-col :span="18" style="padding-left: 20rpx;">
				<uni-easyinput style="background-color: #fff;" suffixIcon="search" v-model="queryParams.number"
					placeholder="报工单号" @iconClick="queryClick"></uni-easyinput>
			</uni-col>
			<uni-col :span="4" style="padding-left: 8rpx;padding-top: 7rpx;">
				<uni-icons type="scan" size="32"></uni-icons>
			</uni-col>
		</uni-row>
		<uni-row>
			<uni-col :span="2" >
				<checkbox-group @change="checkBoxAll">
					<checkbox value="Y"   style="transform:scale(0.7)"/>
				</checkbox-group>
			</uni-col>
			<uni-col :span="4" style="padding-top: 7rpx;">全选</uni-col>
			<uni-col :span="12" >
				<button class="mini-btn" type="primary" size="mini" style="transform:scale(0.9)" @click="spotCheckClick()">抽检</button>
			</uni-col>
		</uni-row>
		<checkbox-group @change="checkBoxGroup">
		<view v-for="(item,index) in reportList" style="margin-top: 10rpx;">
			<uni-row class="resourceList-uni-row">
				<uni-col :span="2">
					<checkbox :value="item.id"  :checked="item.checked" style="transform:scale(0.7)"/>
				</uni-col>
				<uni-col :span="22">
					<text style="font-size: 18px;font-weight:bold;">
						报工单:&nbsp;&nbsp;&nbsp;{{item.number}}
					</text>
				</uni-col>
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<text class="resourceList-text">
					报工时间:&nbsp;&nbsp;&nbsp;{{item.reportTime}}
				</text>
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<text class="resourceList-text">
					报工人:&nbsp;&nbsp;&nbsp;{{item.reportUserName}}
				</text>
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<text class="resourceList-text">
					工单:&nbsp;&nbsp;&nbsp;{{item.workOrderNumber}}
				</text>
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<text class="resourceList-text">
					产品:&nbsp;&nbsp;&nbsp;{{item.materialName}}
				</text>
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<text class="resourceList-text">
					规格型号:&nbsp;&nbsp;&nbsp;{{item.specification}}
				</text>
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<uni-col :span="10">
					<text class="resourceList-text">
						工序:&nbsp;&nbsp;&nbsp;{{item.processName}}
					</text>
				</uni-col>
				<uni-col :span="8">
					<text class="resourceList-text">
						报工数:&nbsp;&nbsp;&nbsp;{{item.reportQuantity}}
					</text>
				</uni-col>
				
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<!-- <text class="resourceList-text">
					合格数:&nbsp;&nbsp;&nbsp;{{item.qualifiedQuantity}}
				</text> -->
			</uni-row>
			<uni-row class="resourceList-uni-row">
				<!-- <uni-col :span="9">
					<text class="resourceList-text">
						结算工资:&nbsp;&nbsp;&nbsp;{{item.wages}}
					</text>
				</uni-col>
				<uni-col :span="5">
					<view v-if="item.isSettle=='Y'" class="tag-view" style="margin-top: 6rpx;">
						<uni-tag :circle="true" text="已结算" type="error" />
					</view>
					<view v-if="item.isSettle=='N'" class="tag-view" style="margin-top: 6rpx;">
						<uni-tag :circle="true" text="未结算" type="error" />
					</view>
				</uni-col> -->
				<uni-col :span="12">
					<text class="resourceList-text">
						备注:&nbsp;&nbsp;&nbsp;{{item.remark}}
					</text>
				</uni-col>
				<uni-col :span="6">
					<view v-if="item.qualityStatus=='A'" class="tag-view" style="margin-top: 6rpx;">
						<uni-tag :circle="true" text="免检" type="success" />
					</view>
					<view v-if="item.qualityStatus=='B'" class="tag-view" style="margin-top: 6rpx;">
						<uni-tag :circle="true" text="待质检" type="warning" />
					</view>
					<view v-if="item.qualityStatus=='C'" class="tag-view" style="margin-top: 6rpx;">
						<uni-tag :circle="true" text="质检通过" type="success" />
					</view>
					<view v-if="item.qualityStatus=='D'" class="tag-view" style="margin-top: 6rpx;">
						<uni-tag :circle="true" text="质检不良" type="error" />
					</view>
				</uni-col>
				<uni-col :span="6">
					<button style="padding: 0rpx; height: 57rpx;width: 120rpx;" plain="true" size="mini" type="primary"
						@click="addReportQuality(item)">
						<text style="font-size: 4;">质检单</text>
					</button>
				</uni-col>
			</uni-row>
			
		</view>
		</checkbox-group>
		<uni-load-more :status="loadStatus"  />
		<reportQualityForm ref="reportQualityForm"> </reportQualityForm>
	</view>

</template>

<script>
	import {
		listReport,
		getReport,
	} from "@/api/production/report";
	import {
		getUserProfile
	} from "@/api/system/user"
	import {
		getDicts
	} from "@/api/system/dict/data";
	import reportQualityForm from "@/pages/quality/reportQuality/form.vue";
	import pdaScan from "@/components/mes/pdaScan.vue"
	export default {
		components: {
			reportQualityForm,
		},
		data() {
			return {
				queryParams: {
					pageSize: 5,
					pageNum: 1
				},
				//报工单列表
				reportList: [],
				user: {},
				loadStatus: "more",
				checkIds:[],
				
			}
		},
		onShow: function() {  
		        /* var _this = this  
		        uni.$off('scancodedate') // 每次进来先 移除全局自定义事件监听器  
		        uni.$on('scancodedate',function(data){  
		            console.log('你想要的code：', data.code)
					  _this.queryParams.number=data.code;
					  _this.queryClick();
		        })  */
		    }  ,
		 onReachBottom() {
			 this.clickLoadMore()
		 },
		created() {
			this.getUser();
			this.getList();
		},
		methods: {
			/** 查询报工单列表 */
			getList() {
				this.queryParams.reportUserId = this.user.userId;
				listReport(this.queryParams).then(reportResponse => {
					let total =reportResponse.total;
					let list = reportResponse.rows;
					//已查询出来的和新查询的 大于等于total总页数，表示no-more
					if (list.length+this.reportList.length >=total) {
						this.loadStatus = "no-more"
					} else {
						this.loadStatus = "more"
					}

					if (this.reportList.length > 0) {
						this.reportList = this.reportList.concat(list)
					} else {
						this.reportList = list;
					}

				})
			},
			getUser() {
				getUserProfile().then(response => {
					this.user = response.data
				})
			},
			//点击新增质检
			addReportQuality(report) {
				this.$refs.reportQualityForm.openPopup(report);
			},
			//点击抽检
			spotCheckClick(){
				let el = [];
				 el =  this.reportList.filter(item => item.checked==true);
				
				if(el.length==0){
					this.$modal.msgError("请选择数据！");
					return;
					
				}
				let tmpReport  = {};
				for (var i = 0; i < el.length; i++) {
					
					//必须时待质检状态的报工单
					if(el[i].qualityStatus!=="B"){
						this.$modal.msgError("请选择待检验的数据！");
						return;
					}
					
					if(i==0){
						tmpReport =el[i]
					}else{
						let materialId = tmpReport.materialId
						let processId = tmpReport.processId
						let specification = tmpReport.specification
						
						console.log("tmpReoirt====>",tmpReport)
						console.log("el[i]====>",el[i])
						if(materialId!=el[i].materialId||processId!=el[i].processId||specification!=el[i].specification){
							this.$modal.msgError("批量抽检的工序、物料、规格必须相同！");
							return ;
							
						}
					}
					
					
				}
				
				this.$refs.reportQualityForm.openPopupBatch(this.checkIds,el[0]);
			},
			queryClick() {
				this.queryParams.pageNum = 1
				this.reportList = []
				this.getList();
			},
			clickLoadMore() {
				if (this.loadStatus === "no-more") {
					return;
				}
				this.loadStatus = "loading"
				this.queryParams.pageNum += 1;
				this.getList();
			},
			checkBoxAll(valueArr){
				let value = valueArr.detail.value[0];
				this.checkIds=[];
				if(value==="Y"){
					//全选
					this.reportList.map(item =>{
						this.checkIds.push(item.id)
						item.checked = true
					})
				}else{
					//取消全选
					this.reportList.map(item =>{
						item.checked = false
					})
					
				}
				this.$forceUpdate();
				
				
			},
			checkBoxGroup(valueArr){
				let ids = valueArr.detail.value;
				console.log("ids===>",ids)
				for (var i = 0; i < this.reportList.length; i++) {
					this.reportList[i].checked=false;
					for (var j = 0; j < ids.length; j++) {
						if(this.reportList[i].id==ids[j]){
							console.log("11111===>",this.reportList)
							this.reportList[i].checked=true;
						}
					}
				}
				this.checkIds=ids;
			}
		},
	}
</script>

<style>
	.resourceList-uni-row {
		margin-bottom: 8rpx;
	}

	.resourceList-text {
		font-size: 16px;
	}

	.resourceList {
		margin-left: 10px;

	}
</style>
